1. Array Simulation. The output produced is as follows. Original List Final List -------------------------------------------------------------- [12, 7] [13, 8] [2, 3, 4, 5, 6] [3, 5, 5, 5, 6] [3, 4, 5, 7, 9] [3, 5, 7, 9, 10] [2, 3, 5, 7, 9] [3, 5, 7, 9, 10] [4, 5, 9, 6, 2] [5, 7, 11, 7, 2] 2. Inheritance. The output produced is as follows. b 1 b 1 d 1 c 1 b 2 b 2 d 2 d 2 d a d d 3. Many solutions possible, one is shown below: public static void adjustTV(Scanner tokenScan){ int volume = 0; boolean on = false; while(tokenScan.hasNext()){ String setting = tokenScan.next(); if(setting.equals("volume")){ int volumeChange = tokenScan.nextInt(); if(on){ volume += volumeChange; if(volume < 0){ volume = 0; } if(volume > 10){ volume = 10; } } } else { //"power" on = !on; } } if(on){ System.out.println("The TV is on"); }else{ System.out.println("The TV is off"); } System.out.println("The volume is " + volume); } 4. Many solutions possible, one is shown below: public static int numUnique(int[] list) { if (list.length == 0) { return 0; } else { int count = 1; for (int i = 1; i < list.length; i++) { if (list[i] != list[i - 1]) { count++; } } return count; } } 5. Several solutions possible, one is shown below: public class Chameleon extends Critter { private int count = 0; public Action getMove(CritterInfo info) { count++; if (count % 3 == 1) { return Action.INFECT; } else if (info.getFront() == Neighbor.EMPTY) { return Action.HOP; } else { return Action.RIGHT; } } public Color getColor() { if (count % 3 == 0) { return Color.RED; } else if (count % 3 == 1) { return Color.WHITE; } else { return Color.BLUE; } } public String toString() { if (count % 3 == 0) { return "R"; } else if (count % 3 == 1) { return "W"; } else { return "B"; } } } 7. Many solutions possible, some are shown below: public static int analyzeParagraphs(Scanner input) { int max = 0; while (input.hasNextLine()) { String line = input.nextLine(); int count = 0; while (!line.equals("

")) { count++; line = input.nextLine(); } System.out.println(count + "-line paragraph"); if (count > max) { max = count; } } return max; } public static int analyzeParagraphs(Scanner input) { int max = 0; int count = 0; while (input.hasNextLine()) { String line = input.nextLine(); if (line.equals("

")) { System.out.println(count + "-line paragraph"); count = 0; } else { count++; } if (count > max) { max = count; } } return max; } 8. Many solutions possible, some are shown below: public static void removeAdjacentMatches(ArrayList list) { for (int i = 0; i < list.size() - 1; i++) { if (list.get(i) == list.get(i + 1)) { list.remove(i + 1); i--; } } } public static void removeAdjacentMatches(ArrayList list) { int i = 0; while (i < list.size() - 1) { if (list.get(i) == list.get(i + 1)) { list.remove(i + 1); } else { i++; } } } 9. Many solutions possible, some are shown below: public static int[] insert(int[] list1, int[] list2, int index) { int[] result = new int[list1.length + list2.length]; for (int i = 0; i < index; i++) { result[i] = list1[i]; } for (int i = 0; i < list2.length; i++) { result[index + i] = list2[i]; } for (int i = index; i < list1.length; i++) { result[list2.length + i] = list1[i]; } return result; } public static int[] insert(int[] list1, int[] list2, int index) { int[] result = new int[list1.length + list2.length]; for (int i = 0; i < result.length; i++) { if (i < index) { result[i] = list1[i]; } else if (i < index + list2.length) { result[i] = list2[i - index]; } else { result[i] = list1[i - list2.length]; } } return result; } 10. Many solutions possible, some are shown below: public static void printReversed(String str) { for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ' ') { System.out.print(" "); } else { int j = i; while (j < str.length() && str.charAt(j) != ' ') { j++; } for (int k = j - 1; k >= i; k--) { System.out.print(str.charAt(k)); } i = j; } } System.out.println(); } public static void printReversed(String str) { Scanner input = new Scanner(str); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ' ') { System.out.print(" "); } else { String word = input.next(); for (int j = word.length() - 1; j >= 0; j--) { System.out.print(word.charAt(j)); } i += word.length() - 1; } } System.out.println(); } public static void printReversed(String str) { String build = ""; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ' ') { if (build.length() != 0) { System.out.print(build); build = ""; } System.out.print(" "); } else { build = str.charAt(i) + build; } } System.out.println(build); }